package com.tanhua.manager.controller;

import com.tanhua.domain.mongo.UserFreeze;
import com.tanhua.manager.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    private ManageService manageService;

    @GetMapping(value = "/users",name = "分页查询用户数据")
    public ResponseEntity queryUsers(@RequestParam(defaultValue = "1")Integer page,
                                     @RequestParam(defaultValue = "10")Integer pagesize,
                                     @RequestParam(required = false)Long id,
                                     @RequestParam(required = false)String nickname,
                                     @RequestParam(required = false)String city
    ){
        return manageService.queryUsers(page,pagesize,id,nickname,city);
    }

    @GetMapping(value = "/users/{userID}",name = "查询用户详细数据")
    public ResponseEntity queryUserById(@PathVariable("userID") Long userId){
        return manageService.queryUserById(userId);
    }


    /**
     * 解决动态审核中前后端state类型不一致
     * @Author ZacharyJo
     * @date 2021/07/17 12:07:47
     */
    @GetMapping(value = "/messages",name = "分页查询指定用户的动态数据")
    public ResponseEntity queryMessages(@RequestParam(defaultValue = "1")Integer page,
                                        @RequestParam(defaultValue = "10")Integer pagesize,
                                        @RequestParam(required = false) Long uid,
                                        @RequestParam(value = "state",required = false) String stateStr)  {
        Integer state = 0;
        if (stateStr==null||stateStr.equals("''")||stateStr.equals("")){
            System.out.println("空数据");
            state = null ;
        }else {
            state = Integer.parseInt(stateStr);
        }
        return manageService.queryMessages(page, pagesize, uid, state);

    }
    @GetMapping(value = "/videos",name = "分页查询指定用户的小视频数据")
    public ResponseEntity queryVideos(@RequestParam(defaultValue = "1")Integer page,
                                      @RequestParam(defaultValue = "10")Integer pagesize,
                                      Long uid){

        return manageService.queryVideos(page,pagesize,uid);
    }

    @GetMapping(value = "/messages/{id}",name = "分页查询指定用户的小视频数据")
    public ResponseEntity queryMovementById(@PathVariable("id") String movementId){
        return manageService.queryMovementById(movementId);
    }

    @GetMapping(value = "/messages/comments",name = "分页查询指定动态的评论")
    public ResponseEntity queryComments(@RequestParam(defaultValue = "1")Integer page,
                                        @RequestParam(defaultValue = "10")Integer pagesize,
                                        @RequestParam("messageID") String movementId){
        return manageService.queryComments(page,pagesize,movementId);
    }


    @PostMapping(value = "/users/freeze",name = "用户冻结操作")
    public ResponseEntity saveFreeze(@RequestBody UserFreeze userFreeze){

        return manageService.saveFreeze(userFreeze);
    }

    @PostMapping(value = "/users/unfreeze",name = "用户解冻操作")
    public ResponseEntity unfreeze(@RequestBody Map map){
        Integer userId = (Integer) map.get("userId");
        String reasonsForThawing = (String) map.get("reasonsForThawing");
        return manageService.unfreeze(userId,reasonsForThawing);
    }

    @PostMapping(value = "/messages/pass")
    public ResponseEntity<?> movementPass(@RequestBody String[] movementIds) {
        return manageService.movementPass(movementIds);
    }
}
